﻿using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.Web;
using System.Text;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Generic functions used throughout the system.
/// </summary>
namespace App_Code
{
    //### All data related functions used in the application
    namespace DataAccess
    {
        public class MSSQL
        {
            public SqlConnection Connect()
            {
                string strConnectString = ConfigurationManager.AppSettings["ConnectionString"];
                SqlConnection con = new SqlConnection(strConnectString);
                return con;
            }

            public DataTable SQLTable(string Query)
            {
                SqlConnection con = Connect();
                con.Open();
                SqlCommand cmd = new SqlCommand(Query, con);

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable Result = new DataTable();

                da.Fill(Result);
                con.Close();

                return Result;
            }

            public void emptyTable(string tablename)
            {
                SqlConnection con = Connect();
                con.Open();

                string strQuery = "DELETE FROM \"" + tablename + "\"";
                SqlCommand cmd = new SqlCommand(strQuery, con);
                cmd.ExecuteNonQuery();

                con.Close();
            }

            public void ExecSP(string Query)
            {
                SqlConnection con = Connect();
                con.Open();

                SqlCommand cmd = new SqlCommand(Query, con);
                cmd.ExecuteNonQuery();

                con.Close();
            }

            public string SimpleQuery(string query)
            {
                SqlConnection con = Connect();
                con.Open();

                SqlCommand cmd = new SqlCommand(query, con);

                SqlDataReader reader = cmd.ExecuteReader();

                string Result = null;

                while (reader.Read())
                {
                    Result = reader[0].ToString();
                }

                reader.Close();
                con.Close();

                return Result;
            }

            public void InsertRecords(string sql)
            {
                //### Execute a passed SQL statement and insets records
                string strConnectString = ConfigurationManager.AppSettings["ConnectionString"];
                SqlConnection con = new SqlConnection(strConnectString);

                con.Open();
                SqlCommand cmd = new SqlCommand(sql, con);

                con.Close();

            }
        }

    }

    //### All common functions used in the application
    namespace Common
    {
        public class Common
        {
        }
    }
}